package com.atung.parking.controller;

import com.atung.parking.config.permission.PermissionValid;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.config.result.PageResponse;
import com.atung.parking.entity.BusParkingIotPackage;
import com.atung.parking.enums.AdminType;
import com.atung.parking.enums.ApplicableScopeType;
import com.atung.parking.enums.PackageSourceType;
import com.atung.parking.enums.ParkingPackageType;
import com.atung.parking.mapstruct.ParkingIotPackageConverter;
import com.atung.parking.request.query.AppQueryParkingIotRequest;
import com.atung.parking.request.query.QueryParkingIotRequest;
import com.atung.parking.request.query.QueryParkingPackageRequest;
import com.atung.parking.request.save.SaveOrUpdateParkingPackageRequest;
import com.atung.parking.request.save.UpdatePackageStateRequest;
import com.atung.parking.response.page.ApprovalRecordResponse;
import com.atung.parking.response.page.ParkingIotPackageResponse;
import com.atung.parking.service.ParkingPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@RestController
@Api(tags = "停车场套餐管理")
@RequestMapping("/api/parkingPackage")
public class ParkingPackageController {


    @Resource
    private ParkingPackageService parkingPackageService;

    @Resource
    private ParkingIotPackageConverter parkingIotPackageConverter;

    @GetMapping("/query")
    @ApiOperation("获取套餐列表")
    @PermissionValid(roleType = {AdminType.ADMIN, AdminType.SUPERADMIN})
    public ApiResult<PageResponse<ParkingIotPackageResponse>> queryPackageList(QueryParkingPackageRequest request) {
        PageInfoResponse<BusParkingIotPackage> pageInfoResponse = parkingPackageService.queryPackageListService(request);
        return ApiResult.ok(pageInfoResponse.getData().stream().map(parkingIotPackageConverter::toResponse)
                .collect(Collectors.toList()), request.getPageNum(), request.getPageSize(), pageInfoResponse.getTotal());
    }

    @PostMapping("/save")
    @ApiOperation("新增套餐")
    @PermissionValid(roleType = {AdminType.ADMIN, AdminType.SUPERADMIN})
    public ApiResult savePackage(@RequestBody @Valid SaveOrUpdateParkingPackageRequest request) {
        return parkingPackageService.saveParkingPackageService(request);
    }

    @PostMapping
    @ApiOperation("修改套餐状态(上架、下架)")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN})
    public ApiResult updatePackageState(@RequestBody @Valid UpdatePackageStateRequest request) {
        return parkingPackageService.updatePackageStateService(request);
    }

    @GetMapping("/query/approval/{id}")
    @ApiOperation("获取车场套餐操作记录")
    @PermissionValid(roleType = {AdminType.SUPERADMIN,AdminType.ADMIN})
    public ApiResult<List<ApprovalRecordResponse>> queryApprovalParkingIotPackageList(@PathVariable("id") Long id) {
        return ApiResult.ok(parkingPackageService.queryApprovalParkingIotPackageListService(id));
    }

    @GetMapping("/queryType")
    @ApiOperation("获取套餐类型下拉数据")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN, AdminType.USER})
    public ApiResult<List<Map<String, String>>> getPackageTypeList() {
        List<Map<String, String>> packageTypeList = new ArrayList<>();
        for (ParkingPackageType packageType : ParkingPackageType.values()) {
            Map<String, String> packageTypeMap = new HashMap<>();
            packageTypeMap.put("name", packageType.getDescription());
            packageTypeMap.put("value", packageType.name());
            packageTypeList.add(packageTypeMap);
        }
        return ApiResult.ok(packageTypeList);
    }

    @GetMapping("/querySource")
    @ApiOperation("获取套餐来源下拉数据")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN, AdminType.USER})
    public ApiResult<List<Map<String, String>>> getPackageSourceList() {
        List<Map<String, String>> packageSourceList = new ArrayList<>();
        for (PackageSourceType packageType : PackageSourceType.values()) {
            Map<String, String> packageTypeMap = new HashMap<>();
            packageTypeMap.put("name", packageType.getDescription());
            packageTypeMap.put("value", packageType.name());
            packageSourceList.add(packageTypeMap);
        }
        return ApiResult.ok(packageSourceList);
    }

    @GetMapping("/queryScope")
    @ApiOperation("获取套餐使用范围下拉数据")
    @PermissionValid(roleType = {AdminType.SUPERADMIN, AdminType.ADMIN, AdminType.USER})
    public ApiResult<List<Map<String, String>>> getPackageScopeList() {
        List<Map<String, String>> scopeList = new ArrayList<>();
        for (ApplicableScopeType scopeType : ApplicableScopeType.values()) {
            Map<String, String> scopeTypeMap = new HashMap<>();
            scopeTypeMap.put("name", scopeType.getDescription());
            scopeTypeMap.put("value", scopeType.name());
            scopeList.add(scopeTypeMap);
        }
        return ApiResult.ok(scopeList);
    }
    @GetMapping("/appDetails/{id}")
    @ApiOperation("小程序获取(套餐详情)")
    public ApiResult appParkingPackageDetails(@PathVariable("id")Long id){
        return parkingPackageService.appParkingPackageDetailsService(id);
    }

    @GetMapping("/appConfirmationDetails/{id}")
    @ApiOperation("小程序获取(确认订单详情)")
    public ApiResult appParkingPackageConfirmationDetails(@PathVariable("id")Long id){
        return parkingPackageService.appParkingPackageConfirmationDetailsService(id);
    }
    @GetMapping("/query/available/time/{id}")
    @ApiOperation("小程序获取套餐可以时间")
    public ApiResult queryAvailableTime(@PathVariable("id")Long id){
        return parkingPackageService.queryAvailableTimeService(id);
    }
    @GetMapping("/export")
    @ApiOperation("导出车场套餐数据")
//    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public void exportParkingIotPackage(QueryParkingPackageRequest request, HttpServletResponse httpServletResponse) {
        parkingPackageService.exportParkingIotPackageService(request, httpServletResponse);
    }
}
